{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Replace All Digits with Characters"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: replaceDigits"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #将所有数字用字符替换"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个下标从 <strong>0</strong> 开始的字符串 <code>s</code> ，它的 <strong>偶数</strong> 下标处为小写英文字母，<strong>奇数</strong> 下标处为数字。</p>\n",
    "\n",
    "<p>定义一个函数 <code>shift(c, x)</code> ，其中 <code>c</code> 是一个字符且 <code>x</code> 是一个数字，函数返回字母表中 <code>c</code> 后面第 <code>x</code> 个字符。</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>比方说，<code>shift('a', 5) = 'f'</code> 和 <code>shift('x', 0) = 'x'</code> 。</li>\n",
    "</ul>\n",
    "\n",
    "<p>对于每个 <strong>奇数</strong> 下标 <code>i</code> ，你需要将数字 <code>s[i]</code> 用 <code>shift(s[i-1], s[i])</code> 替换。</p>\n",
    "\n",
    "<p>请你替换所有数字以后，将字符串 <code>s</code> 返回。题目 <strong>保证</strong><em> </em><code>shift(s[i-1], s[i])</code> 不会超过 <code>'z'</code> 。</p>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>s = \"a1c1e1\"\n",
    "<b>输出：</b>\"abcdef\"\n",
    "<strong>解释：</strong>数字被替换结果如下：\n",
    "- s[1] -&gt; shift('a',1) = 'b'\n",
    "- s[3] -&gt; shift('c',1) = 'd'\n",
    "- s[5] -&gt; shift('e',1) = 'f'</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>s = \"a1b2c3d4e\"\n",
    "<b>输出：</b>\"abbdcfdhe\"\n",
    "<strong>解释：</strong>数字被替换结果如下：\n",
    "- s[1] -&gt; shift('a',1) = 'b'\n",
    "- s[3] -&gt; shift('b',2) = 'd'\n",
    "- s[5] -&gt; shift('c',3) = 'f'\n",
    "- s[7] -&gt; shift('d',4) = 'h'</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= s.length &lt;= 100</code></li>\n",
    "\t<li><code>s</code> 只包含小写英文字母和数字。</li>\n",
    "\t<li>对所有 <strong>奇数</strong> 下标处的 <code>i</code> ，满足 <code>shift(s[i-1], s[i]) &lt;= 'z'</code> 。</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [replace-all-digits-with-characters](https://leetcode.cn/problems/replace-all-digits-with-characters/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [replace-all-digits-with-characters](https://leetcode.cn/problems/replace-all-digits-with-characters/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"a1c1e1\"', '\"a1b2c3d4e\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "\n",
    "        return \"\".join([chr(ord(s[i-1]) + int(s[i])) if i % 2 else s[i] for i in range(len(s))])\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        res =[]\n",
    "        for i in range(len(s)):\n",
    "            if i%2!=0:\n",
    "                temp =chr(ord(s[i-1])+int(s[i]))\n",
    "                res.append(temp)\n",
    "            else:\n",
    "                res.append(s[i])\n",
    "        return \"\".join(res) \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        s = list(s)\n",
    "        n = len(s)\n",
    "        for i in range(1,n,2):\n",
    "            if s[i].isdigit():\n",
    "                s[i] = chr(ord(s[i-1]) + int(s[i]))\n",
    "        return ''.join(s)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        for i in range(len(s)):\n",
    "            if s[i].isdigit():\n",
    "                s=s[:i]+chr(ord(s[i-1])+int(s[i]))+s[i+1:]\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        return \"\".join(chr(ord(s[k-1]) + int(v)) if k % 2 else v for k,v in enumerate(s))\n",
    "        # ans = \"\"\n",
    "        # for k,v in enumerate(s):\n",
    "        #     if k % 2:\n",
    "        #         ans += chr(ord(s[k-1]) + int(v))\n",
    "        #     else:\n",
    "        #         ans += v\n",
    "        # return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        s = list(s)\n",
    "        n = len(s)\n",
    "        for i in range(1,n,2):\n",
    "            if s[i].isdigit():\n",
    "                s[i] = chr(ord(s[i-1]) + int(s[i]))\n",
    "        return ''.join(s)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        ans=s[0]\n",
    "        def shift(c,x):\n",
    "            return chr(ord(c)+x)\n",
    "        for i in range(1,len(s)):\n",
    "            if '0'<=s[i]<='9':\n",
    "                ans+=shift(s[i-1],int(s[i]))\n",
    "            else:\n",
    "                ans+=s[i]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        ans = \"\"\n",
    "        for i in range(len(list(s))):\n",
    "            if i % 2 == 0:\n",
    "                ans += s[i]\n",
    "            else:\n",
    "                ans += chr(ord(s[i-1])+int(s[i]))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        result=list(s)\n",
    "        for i in range(1,len(s),2):\n",
    "            result[i]=chr(ord(s[i-1])+int(s[i]))\n",
    "        return \"\".join(result)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        s = list(s)\n",
    "        for i in range(1, len(s), 2):\n",
    "            s[i] = chr(ord(s[i - 1]) + int(s[i]))\n",
    "        return \"\".join(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        ls = list(s)\n",
    "        for i in range(1,len(ls),2):\n",
    "            ls[i] = chr(ord(ls[i-1])+int(ls[i]))\n",
    "        return ''.join(ls)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        l = []\n",
    "        for i in range(len(s)):\n",
    "            if i % 2 == 0:\n",
    "                l.append(s[i])\n",
    "\n",
    "            else:\n",
    "                l.append(chr(ord(s[i - 1]) + int(s[i])))\n",
    "        return \"\".join(l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        ret = []\n",
    "        for i in range(len(s)):\n",
    "            if s[i].isdigit():\n",
    "                ret.append(chr(ord(s[i - 1]) + int(s[i])))\n",
    "            else:\n",
    "                ret.append(s[i])\n",
    "        return ''.join(ret)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        arr = list(s)\n",
    "        def shift(c,x):\n",
    "            alphabet = ('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z')\n",
    "            return alphabet[alphabet.index(c) + int(x)]\n",
    "        for i in range(1,len(s),2):\n",
    "            arr[i] = shift(arr[i-1],arr[i])\n",
    "        return ''.join(arr)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        # return \"\".join(chr(ord(s[k-1]) + int(v)) if k % 2 else v for k,v in enumerate(s))\n",
    "        ans = \"\"\n",
    "        for k,v in enumerate(s):\n",
    "            if k % 2:\n",
    "                ans += chr(ord(s[k-1]) + int(v))\n",
    "            else:\n",
    "                ans += v\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "def shift(c, x):\n",
    "    return chr(ord(c) + x)\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        ans = list(s)\n",
    "        for i in range(1, len(ans), 2):\n",
    "            digit = ans[i]\n",
    "            letter = ans[i-1]\n",
    "            ans[i]=shift(letter, int(digit))\n",
    "        return ''.join(ans)       \n",
    "\n",
    "\n",
    "'''\n",
    "ord(c)：这个函数返回字符 c 的 ASCII 值。例如，ord('A') 返回的是 65，因为大写字母 A 的 ASCII 值是 65。\n",
    "chr(n)：这个函数接受一个整数 n，并返回对应的 ASCII 字符。例如，chr(65) 返回的是字符 'A'，因为 65 对应于大写字母 A 的 ASCII 值。\n",
    "chr(ord(c) + x)：这个表达式将字符 c 的 ASCII 值加上整数 x，然后将结果转换回字符。这就实现了字符的加密操作。\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        def shift(c,x):\n",
    "            return chr(ord(c)+int(x))\n",
    "        new_str=''\n",
    "        for i in range(0,len(s)):\n",
    "            c= (i%2==1) and shift(s[i-1],s[i]) or s[i]\n",
    "            new_str+=c\n",
    "        return new_str"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        if len(s) == 1:\n",
    "            return s\n",
    "        ls = list(s)\n",
    "        for i in range(1,len(ls),2):\n",
    "            ls[i] = chr(ord(ls[i-1])+int(ls[i]))\n",
    "        return ''.join(ls)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        lst = list(s)\n",
    "        for i in range(1, len(s), 2):\n",
    "            lst[i] = chr(ord(lst[i - 1]) + int(lst[i]))\n",
    "        return \"\".join(lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        ans=list(s)\n",
    "        n=len(ans)\n",
    "        for i in range(1,n,2):\n",
    "            ans[i]=chr(ord(ans[i-1])+int(ans[i]))\n",
    "        return ''.join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        result=[]\n",
    "        for i in range(len(s)):\n",
    "            if s[i].isdigit():\n",
    "                result.append(chr(ord(s[i-1])+int(s[i])))\n",
    "            else:\n",
    "                result.append(s[i])\n",
    "        return''.join(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        a = list('abcdefghijklmnopqrstuvwxyz')\n",
    "        s = list(s)\n",
    "        for i in range(1,len(s)):\n",
    "            if s[i].isdigit():\n",
    "                s[i] = a[a.index(s[i-1])+int(s[i])]\n",
    "        return ''.join(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        if len(s)==1: return s\n",
    "        record = list(s)\n",
    "        for i in range(1,len(record),2):\n",
    "            record[i] = chr(ord(record[i-1])+int(record[i]))\n",
    "        \n",
    "        return \"\".join(record)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        rep = list(s)\n",
    "        for i in range(1 , len(s) , 2):\n",
    "            rep[i] = chr(ord(rep[i - 1]) + int(rep[i]))\n",
    "        return \"\".join(rep)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        n = len(s)\n",
    "        ans = ''\n",
    "        for i, ch in enumerate(s):\n",
    "            if i % 2 == 1:\n",
    "                ans += chr(ord(s[i - 1]) + int(ch))\n",
    "            else:\n",
    "                ans += ch\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        s = list(s)\n",
    "        for i in range(0, len(s), 2):\n",
    "            a = s[i]\n",
    "            if i + 1 < len(s):\n",
    "                b = int(s[i + 1])\n",
    "                s[i + 1] = chr(ord(a) + b)\n",
    "        return ''.join(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        n = len(s)\n",
    "        arr = list(s)\n",
    "        for i in range(1,n,2):\n",
    "            arr[i] = chr(ord(arr[i-1])+int(arr[i]))\n",
    "        return \"\".join(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# class Solution:\n",
    "#     def replaceDigits(self, s: str) -> str:\n",
    "#         res=''\n",
    "#         n=len(s)\n",
    "#         for i in range(0,n,2):\n",
    "#             res+=s[i]\n",
    "#             if i+1<n:\n",
    "#                 res+=chr(ord(s[i])+int(s[i+1]))\n",
    "#         return res\n",
    "\n",
    "### 官解：先变列表，直接修改奇数位\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        n = len(s)\n",
    "        arr = list(s)\n",
    "        for i in range(1, n, 2):\n",
    "            arr[i] = chr(ord(arr[i-1]) + int(arr[i]))\n",
    "        return \"\".join(arr)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        return \"\".join(chr(ord(s[k-1]) + int(v)) if k % 2 else v for k,v in enumerate(s))\n",
    "        # ans = \"\"\n",
    "        # for k,v in enumerate(s):\n",
    "        #     if k % 2:\n",
    "        #         ans += chr(ord(s[k-1]) + int(v))\n",
    "        #     else:\n",
    "        #         ans += v\n",
    "        # return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        def shift(c,x):\n",
    "            return chr(ord(c)+x)\n",
    "        res=''\n",
    "        for i,c in enumerate(s):\n",
    "            if c.isdigit():\n",
    "                c=shift(s[i-1],int(c))\n",
    "            res+=c\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        length, data = len(s), list(s)\n",
    "        for _ in range(1, length, 2):\n",
    "            data[_] = chr(ord(data[_ - 1]) + int(data[_]))\n",
    "        return ''.join(data)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        s = list(s)\n",
    "        for i in range(1, len(s), 2):\n",
    "            s[i] = chr(ord(s[i-1]) + int(s[i]))\n",
    "        return ''.join(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "       for i in range(len(s)):\n",
    "         if s[i].isnumeric():\n",
    "           s=s.replace(s[i],chr(ord(s[i-1]) +int(s[i])),1)\n",
    "       return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        str_list = list(s)\n",
    "        for index in range(1, len(s), 2):\n",
    "            str_list[index] = chr(ord(str_list[index - 1]) + int(str_list[index]))\n",
    "        return ''.join(str_list)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        def shift(ch, x) -> str:\n",
    "            return str(chr(ord(ch) + int(x)))\n",
    "        s = list(s)\n",
    "        for i in range(len(s)):\n",
    "            if i % 2 == 1:\n",
    "                s[i] = shift(s[i - 1], s[i])\n",
    "        return ''.join(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        res = \"\"\n",
    "        for i, ss in enumerate(s):\n",
    "            if i % 2 == 0:\n",
    "                res += ss\n",
    "            else:\n",
    "                res += chr(ord(s[i - 1]) + int(ss))\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        def shift(c,x):\n",
    "            return chr(ord(c)+int(x))\n",
    "        new_str=''\n",
    "        for i in range(0,len(s)):\n",
    "            c= (i%2==1) and shift(s[i-1],s[i]) or s[i]\n",
    "            new_str+=c\n",
    "        return new_str"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        s = list(s)\n",
    "        for i in range(0, len(s)-1, 2):\n",
    "            s[i + 1] = chr(ord(s[i]) + int(s[i+1]))\n",
    "        return ''.join(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        ans=''\n",
    "        for i in range(0,len(s)-len(s)%2,2):\n",
    "            ans+=s[i]\n",
    "            ans+=chr(ord(s[i])+int(s[i+1]))\n",
    "        if len(s)%2==1:\n",
    "            ans+=s[-1]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        s2=''\n",
    "        for i in range(len(s)):\n",
    "            if i%2==0:\n",
    "                s2+=s[i]\n",
    "            else:\n",
    "                s2+=chr(ord(s[i-1])+int(s[i]))\n",
    "        return s2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        n = len(s)\n",
    "        arr = list(s)\n",
    "        for i in range(1, n, 2):\n",
    "            arr[i] = chr(ord(arr[i-1]) + int(arr[i]))\n",
    "        return ''.join(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        ans = list(s)\n",
    "        for i in range(1, len(s), 2):\n",
    "            word = s[i - 1]\n",
    "            num = s[i]\n",
    "            ans[i] = word\n",
    "            ans[i] = chr(ord(word) + int(num))\n",
    "        return \"\".join(ans)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        n = len(s)\n",
    "        a_num = ord('a')\n",
    "        res = ''\n",
    "        for i in range(0, n, 2):\n",
    "            #print(i)\n",
    "            #print(s[i])            \n",
    "            if i + 1 <= n - 1:\n",
    "                res += s[i] + chr(ord(s[i]) + int(s[i + 1]))\n",
    "            else:\n",
    "                res += s[i]\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        def shift(c: str, x: int) -> str:\n",
    "            return chr(ord(c) + x)\n",
    "\n",
    "        l = list(s)\n",
    "        for i in range(1, len(l), 2):\n",
    "            l[i] = shift(l[i - 1], int(l[i]))\n",
    "        return \"\".join(l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        new_s = list(s)\n",
    "        for i in range(1, len(s), 2):\n",
    "            new_s[i] = chr(ord(s[i - 1]) + int(s[i]))\n",
    "\n",
    "        return ''.join(new_s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        x = ''\n",
    "        for i in range(0, len(s)-1, 2):\n",
    "            x += s[i]\n",
    "            x += chr(ord(s[i])+int(s[i+1]))\n",
    "        if len(s) % 2 == 1:\n",
    "            x += s[-1]\n",
    "        return x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        list1 = list(s)\n",
    "        def shift(c, x):\n",
    "            return chr(ord(c)+int(x))\n",
    "        for i in range(len(list1)):\n",
    "            if i%2==1:\n",
    "                list1[i]=shift(list1[i-1],list1[i])\n",
    "        return (\"\".join(list1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        ans = list(s)\n",
    "        for i in range(1, len(s), 2):\n",
    "            word = s[i - 1]\n",
    "            num = s[i]\n",
    "            ans[i] = word\n",
    "            ans[i] = chr(ord(word) + int(num))\n",
    "        return \"\".join(ans)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        n = len(s)\n",
    "        arr = list(s)\n",
    "        for i in range(1, n , 2) :\n",
    "            arr[i] = chr(ord(arr[i-1])+int(arr[i]))\n",
    "        return \"\".join(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        def shift(c: str, x: int) -> str:\n",
    "            return chr(ord(c) + x)\n",
    "\n",
    "        l = list(s)\n",
    "        for i in range(1, len(l), 2):\n",
    "            l[i] = shift(l[i - 1], int(l[i]))\n",
    "        return \"\".join(l)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "    #     s_list = list(s)\n",
    "    #     for i in range(len(s)):\n",
    "    #         if s_list[i].isdigit():\n",
    "    #             s_list[i] = self.shift(s_list[i - 1], int(s_list[i]))\n",
    "    #     return \"\".join(s_list)\n",
    "                \n",
    "    # def shift(self, c: str, x : int):\n",
    "    #     return chr(ord(c) + x)\n",
    "\n",
    "        s_list = list(s)\n",
    "        for i in range(1, len(s), 2):\n",
    "            s_list[i] = chr(ord(s_list[i - 1]) + int(s_list[i]))\n",
    "        return \"\".join(s_list)\n",
    "#方法一：直接根据循环判断，替换 \n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        s.split()\n",
    "        l=list(s)\n",
    "        n=len(l)\n",
    "        for i in range(1,n,2):\n",
    "            l[i]=chr(ord(l[i-1])+int(l[i]))\n",
    "        return \"\".join(l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        sl=list(s)\n",
    "        for i in range(1,len(sl),2):\n",
    "            if sl[i].isdigit():             \n",
    "                sl[i]=chr(ord(sl[i-1])+int(s[i]))\n",
    "        return ''.join(sl)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        n = len(s)\n",
    "        arr = list(s)\n",
    "        for i in range(1, n, 2):\n",
    "            arr[i] = chr(ord(arr[i-1]) + int(arr[i]))\n",
    "        return \"\".join(arr)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        ans = \"\"\n",
    "        for i in range(0, len(s) & ~1, 2):\n",
    "            ans += s[i] + chr(ord(s[i]) + int(s[i+1]))\n",
    "        return ans + (s[-1] if len(s) & 1 else \"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def shift(self,c:str,x:int)->str:\n",
    "        a=ord(c)+x\n",
    "        return chr(a)\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        n=len(s)\n",
    "        nums=list(s)\n",
    "        res=[]\n",
    "        for i in range(1,n,2):\n",
    "            ans=self.shift(s[i-1],int(s[i]))\n",
    "            nums[i]=ans\n",
    "        # print(res)\n",
    "        return ''.join(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        s_list = list(s)\n",
    "        for i in range(len(s)):\n",
    "            if s_list[i].isdigit():\n",
    "                s_list[i] = self.shift(s_list[i - 1], int(s_list[i]))\n",
    "        return \"\".join(s_list)\n",
    "                \n",
    "    def shift(self, c: str, x : int):\n",
    "        return chr(ord(c) + x)\n",
    "\n",
    "        \n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        def shift(c,x):\n",
    "            return chr(ord(c)+int(x))\n",
    "        res=''\n",
    "        for i in range(len(s)):\n",
    "            if s[i].islower():\n",
    "                res=res+s[i]\n",
    "            else:\n",
    "                res=res+shift(s[i-1],s[i])\n",
    "        return res \n",
    "            \n",
    "\n",
    "         \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        arr = list(s)\n",
    "        for i in range(1,len(s),2):\n",
    "            a = chr(ord(s[i-1])+int(s[i]))\n",
    "            arr[i] = a\n",
    "        return ''.join(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        def shift(c: str, x: int) -> str:\n",
    "            return chr(ord(c) + x)\n",
    "        ans = \"\"\n",
    "        for i, c in enumerate(s):\n",
    "            if i % 2 == 1:\n",
    "                ans += shift(s[i-1], int(s[i]))\n",
    "            else:\n",
    "                ans += c\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        for i in range(1, len(s), 2):\n",
    "            s = s[:i] + chr(ord(s[i-1]) + int(s[i])) + s[i+1:]\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        n = len(s)\n",
    "        arr = list(s)\n",
    "        for i in range(1, n, 2):\n",
    "            arr[i] = chr(ord(arr[i - 1]) + int(arr[i]))\n",
    "        return  \"\".join(arr)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        # x = ''\n",
    "        # for i in range(len(s)):\n",
    "        #     x += s[i] if i%2 == 0 else chr(ord(s[i-1]) + int(s[i]))\n",
    "        return ''.join([s[i] if i%2 == 0 else chr(ord(s[i-1]) + int(s[i])) for i in range(len(s))])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def replaceDigits(self, s: str) -> str:\n",
    "        x = ''\n",
    "        for i in range(len(s)):\n",
    "            x += s[i] if i%2 == 0 else chr(ord(s[i-1]) + int(s[i]))\n",
    "        return x"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
